001    /**
002     * Java Gui Builder - A library to build GUIs using an XML file.
003     * Copyright 2002, 2003 (C) François Beausoleil
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package jgb.builder.utils;
021    
022    import org.xml.sax.Attributes;
023    
024    import java.util.Collection;
025    import java.util.HashSet;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
031     */
032    public class AttributesMapAdapter implements Map {
033        private Attributes atts;
034    
035        public AttributesMapAdapter(Attributes atts) {
036            this.atts = atts;
037        }
038    
039        public void setAttributes(Attributes atts) {
040            this.atts = atts;
041        }
042    
043        public void clear() {
044            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "clear" + " operation");
045        }
046    
047        public boolean containsKey(Object key) {
048            return get(key) != null;
049        }
050    
051        public boolean containsValue(Object value) {
052            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "containsValue(Object value)" + " operation");
053        }
054    
055        public Set entrySet() {
056            Set entries = new HashSet();
057            for (int i = 0; i < atts.getLength(); i++) {
058                Map.Entry entry = new MapEntry(atts, i);
059                entries.add(entry);
060            }
061    
062            return entries;
063        }
064    
065        public Object get(Object key) {
066            return atts.getValue((String)key);
067        }
068    
069        public boolean isEmpty() {
070            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "isEmpty()" + " operation");
071        }
072    
073        public Set keySet() {
074            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "keySet()" + " operation");
075        }
076    
077        public Object put(Object key, Object value) {
078            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "put(Object key, Object value)" + " operation");
079        }
080    
081        public void putAll(Map t) {
082            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "putAll(Map t)" + " operation");
083        }
084    
085        public Object remove(Object key) {
086            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "remove(Object key)" + " operation");
087        }
088    
089        public int size() {
090            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "size()" + " operation");
091        }
092    
093        public Collection values() {
094            throw new UnsupportedOperationException("AttributesMapAdapter does not support the " + "values()" + " operation");
095        }
096    
097        private static class MapEntry implements Map.Entry {
098            private Attributes atts;
099            private int index;
100    
101            public MapEntry(Attributes atts, int index) {
102                this.atts = atts;
103                this.index = index;
104            }
105    
106            public boolean equals(Object obj) {
107                boolean equal = false;
108                if (obj != null && obj instanceof MapEntry) {
109                    MapEntry equalTo = (MapEntry)obj;
110                    equal = (this.atts == equalTo.atts)
111                            && (this.index == equalTo.index);
112                }
113    
114                return equal;
115            }
116    
117            public int hashCode() {
118                return 17 + (37 * index * atts.hashCode());
119            }
120    
121            public Object getKey() {
122                return atts.getQName(index);
123            }
124    
125            public Object getValue() {
126                return atts.getValue(index);
127            }
128    
129            public Object setValue(Object value) {
130                throw new UnsupportedOperationException("AttributesMapAdapter.MapEntry does not support the setValue(value) operation");
131            }
132        }
133    }